<!DOCTYPE html>
<html lang="zh-CN" color-mode=light>


<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>使用`@Transactional(propagation = Propagation.NOT_SUPPORTED)`实现事务排除 - 树朾的开源技术分享</title>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate" />
  <meta name="keywords" content="开源, 分享">
  <meta name="description" content="在Spring框架中，`@Transactional`...">
  <meta name="author" content="scwang90">
  <link rel="icon" href="/images/icons/favicon.ico">
  
  
  
  
  

  
<link rel="stylesheet" href="/css/style.css">


  
    
<link rel="stylesheet" href="https://at.alicdn.com/t/font_1445822_p6ry5n7lrr.css">

  

  
    
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  

  
    
      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css" name="highlight-style" mode="light">

      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/solarized-dark.min.css" name="highlight-style" mode="dark">

      
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      loading: {
        gif: '/images/theme/loading.gif',
        lottie: ''
      },
      lazyload: {
        enable: true,
        only_post: 'false',
        loading: {
          gif: '/images/theme/loading.gif',
          lottie: ''
        }
      },
      donate: {
        enable: false,
        alipay: '/images/donate/pay_alipay.jpg',
        wechat: '/images/donate/pay_wxpay.jpg',
        tencent: '/images/donate/pay_tencent.jpg'
      },
      galleries: {
        enable: true
      },
      fab: {
        enable: true,
        always_show: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: false
      },
      preview: {
        background: {
          default: '',
          api: ''
        },
        motto: {
          default: '我在开了灯的床头下，想问问自己的心啊。',
          typing: true,
          api: 'https://v2.jinrishici.com/one.json',
          data_contents: '["data","content"]'
        },
      },
      qrcode: {
        enable: true,
        type: 'url',
        image: 'https://pic.izhaoo.com/weapp-code.jpg',
      },
      toc: {
        enable: true
      },
      scrollbar: {
        type: 'default'
      },
      notification: {
        enable: false,
        delay: 4500,
        list: '',
        page_white_list: '',
        page_black_list: ''
      },
      search: {
        enable: false,
        path: ''
      }
    }
  </script>

  

  

<meta name="generator" content="Hexo 6.2.0"></head>

<body class="lock-screen">
  <div class="loading" id="loading"></div>
  
    


  <nav class="navbar">
    <div class="left">
      
        <i class="iconfont iconhome j-navbar-back-home"></i>
      
      
        <i class="iconfont iconqrcode j-navbar-qrcode"></i>
      
      
        <i class="iconfont iconmoono" id="color-toggle" color-toggle="light"></i>
      
      
    </div>
    <div class="center">使用`@Transactional(propagation = Propagation.NOT_SUPPORTED)`实现事务排除</div>
    <div class="right">
      <i class="iconfont iconmenu j-navbar-menu"></i>
    </div>
    
      <div id="qrcode-navbar"></div>
    
  </nav>

  
  

<nav class="menu">
  <div class="menu-container">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content"><li class="menu-item">
        <a href="/ " class="underline "> 首页</a>
      </li><li class="menu-item">
        <a href="/galleries/ " class="underline "> 摄影</a>
      </li><li class="menu-item">
        <a href="/archives/ " class="underline "> 归档</a>
      </li><li class="menu-item">
        <a href="/tags/ " class="underline "> 标签</a>
      </li><li class="menu-item">
        <a href="/categories/ " class="underline "> 分类</a>
      </li><li class="menu-item">
        <a href="/about/ " class="underline "> 关于</a>
      </li></ul>
    
      <div class="menu-copyright"><p>Powered by <a target="_blank" href="https://github.com/scwang90">scwang90</a>  |  Theme - <a target="_blank" href="https://github.com/izhaoo/hexo-theme-zhaoo">zhaoo</a></p> <p><a target="_blank" href="http://beian.miit.gov.cn/"><span>黔ICP备2021004317号-1</span></a><span>  |  </span><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img class="lazyload" data-original="/images/icons/icon-filing.png" src="https://www.beian.gov.cn/img/new/gongan.png"/> <span> 贵公网安备 52010202002539号</span></a></p></div>
    
  </div>
</nav>
  <main id="main">
  <div class="article-wrap">
    
      <div class="row container container-lg">
        <div class="col-xl-2"></div>
        <div class="col-xl-8"><article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="/images/theme/post-image.jpg" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">使用`@Transactional(propagation = Propagation.NOT_SUPPORTED)`实现事务排除</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>四月 07, 2025</span>
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>2670</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content article-entry">
        
          
        
        <p>在Spring框架中，<code>@Transactional</code>注解是管理事务的常用方式。然而，在某些复杂的业务场景中，我们可能需要对事务进行更精细的控制，例如在父事务中排除某些子事务的参与。本文将探讨如何通过<code>@Transactional(propagation = Propagation.NOT_SUPPORTED)</code>实现事务排除，并结合实际案例进行详细说明。</p>
<h2 id="一、-Transactional注解与事务传播行为"><a href="#一、-Transactional注解与事务传播行为" class="headerlink" title="一、@Transactional注解与事务传播行为"></a>一、<code>@Transactional</code>注解与事务传播行为</h2><p><code>@Transactional</code>注解是Spring中用于声明式事务管理的核心注解。它通过事务传播行为（<code>propagation</code>）属性来控制事务的执行方式。事务传播行为有以下几种：</p>
<ul>
<li><code>REQUIRED</code>（默认值）：如果当前存在事务，则加入该事务；否则创建一个新事务。</li>
<li><code>SUPPORTS</code>：如果当前存在事务，则加入该事务；否则以非事务方式执行。</li>
<li><code>MANDATORY</code>：如果当前存在事务，则加入该事务；否则抛出异常。</li>
<li><code>REQUIRES_NEW</code>：创建一个新事务，如果当前存在事务，则暂停当前事务。</li>
<li><code>NOT_SUPPORTED</code>：以非事务方式执行，如果当前存在事务，则暂停当前事务。</li>
<li><code>NEVER</code>：以非事务方式执行，如果当前存在事务，则抛出异常。</li>
<li><code>NESTED</code>：如果当前存在事务，则在嵌套事务中执行；否则创建一个新事务。</li>
</ul>
<p>在本文中，我们将重点关注<code>NOT_SUPPORTED</code>传播行为，它允许我们在某个方法中明确排除事务的参与。</p>
<h2 id="二、实际场景：父事务中排除部分子事务"><a href="#二、实际场景：父事务中排除部分子事务" class="headerlink" title="二、实际场景：父事务中排除部分子事务"></a>二、实际场景：父事务中排除部分子事务</h2><p>假设我们有一个父Service，其方法依次调用了四个子Service的方法。父Service的方法被<code>@Transactional</code>注解标记，因此整个流程默认在一个事务中执行。然而，现在有一个需求：当第四个子Service方法报错时，只回滚第一个和第三个子Service方法的操作，而第二个子Service方法的操作不应被回滚。</p>
<h2 id="三、解决方案：使用-Transactional-propagation-Propagation-NOT-SUPPORTED"><a href="#三、解决方案：使用-Transactional-propagation-Propagation-NOT-SUPPORTED" class="headerlink" title="三、解决方案：使用@Transactional(propagation = Propagation.NOT_SUPPORTED)"></a>三、解决方案：使用<code>@Transactional(propagation = Propagation.NOT_SUPPORTED)</code></h2><p>为了实现上述需求，我们可以在第二个子Service方法上使用<code>@Transactional(propagation = Propagation.NOT_SUPPORTED)</code>注解。这样，当父Service调用该方法时，当前事务将被暂停，该方法将以非事务方式执行，其操作不会受到父事务回滚的影响。</p>
<p>以下是具体的代码实现：</p>
<h3 id="1-父Service"><a href="#1-父Service" class="headerlink" title="1. 父Service"></a>1. 父Service</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ParentService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ChildService childService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transactional</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">parentMethod</span><span class="params">()</span> &#123;</span><br><span class="line">        childService.method1(); <span class="comment">// 第一个方法，参与父事务</span></span><br><span class="line">        childService.method2(); <span class="comment">// 第二个方法，不参与父事务</span></span><br><span class="line">        childService.method3(); <span class="comment">// 第三个方法，参与父事务</span></span><br><span class="line">        childService.method4(); <span class="comment">// 第四个方法，可能会报错</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-子Service"><a href="#2-子Service" class="headerlink" title="2. 子Service"></a>2. 子Service</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ChildService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transactional</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 方法1的逻辑，参与父事务</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Executing method1 with transaction&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transactional(propagation = Propagation.NOT_SUPPORTED)</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 方法2的逻辑，不参与父事务</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Executing method2 without transaction&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transactional</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method3</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 方法3的逻辑，参与父事务</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Executing method3 with transaction&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transactional</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method4</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="comment">// 方法4的逻辑，可能会报错</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Executing method4 with transaction&quot;</span>);</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Exception</span>(<span class="string">&quot;Method 4 failed&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-测试与结果"><a href="#3-测试与结果" class="headerlink" title="3. 测试与结果"></a>3. 测试与结果</h3><p>假设我们在数据库中进行了一些操作，以下是执行<code>parentMethod</code>时的预期结果：</p>
<ul>
<li><strong>方法1</strong>：参与父事务，如果父事务回滚，其操作将被回滚。</li>
<li><strong>方法2</strong>：不参与父事务，即使父事务回滚，其操作也不会被回滚。</li>
<li><strong>方法3</strong>：参与父事务，如果父事务回滚，其操作将被回滚。</li>
<li><strong>方法4</strong>：参与父事务，如果抛出异常，将导致父事务回滚。</li>
</ul>
<p>通过<code>@Transactional(propagation = Propagation.NOT_SUPPORTED)</code>，我们成功地将方法2从父事务中排除，实现了部分事务的独立性。</p>
<h2 id="四、总结"><a href="#四、总结" class="headerlink" title="四、总结"></a>四、总结</h2><p>在Spring中，<code>@Transactional(propagation = Propagation.NOT_SUPPORTED)</code>是一个强大的工具，可以在复杂的业务场景中实现事务的精细控制。通过暂停当前事务并以非事务方式执行特定方法，我们可以确保某些操作不会受到父事务回滚的影响。这种技术在实际开发中非常有用，尤其是在需要部分事务独立性的情况下。</p>

      </section>
      <section class="extra">
        
          <ul class="copyright">
  
    <li><strong>本文作者：</strong>scwang90</li>
    <li><strong>本文链接：</strong><a href="https://blog.scwang90.cn/2025/04/07/spring-boot-transactional/index.html" title="https:&#x2F;&#x2F;blog.scwang90.cn&#x2F;2025&#x2F;04&#x2F;07&#x2F;spring-boot-transactional&#x2F;index.html">https:&#x2F;&#x2F;blog.scwang90.cn&#x2F;2025&#x2F;04&#x2F;07&#x2F;spring-boot-transactional&#x2F;index.html</a></li>
    <li><strong>版权声明：</strong>本分享所有文章均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" title="BY-NC-SA" target="_blank" rel="noopener">BY-NC-SA</a> 许可协议，转载请注明出处！</li>
  
</ul>
        
        
        
  <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/" rel="tag">spring</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BA%8B%E5%8A%A1/" rel="tag">事务</a></li></ul> 

        
  <nav class="nav">
    <a></a>
    <a href="/2025/01/17/maven-local-jar/">Maven 添加本地Jar依赖 打包报错 Fatal error compiling： Illegal char <*> at index<i class="iconfont iconright"></i></a>
  </nav>

      </section>
      
    </section>
  </div>
</article></div>
        <div class="col-xl-2">
          
            
  <aside class="toc-wrap">
    <h3 class="toc-title">文章目录：</h3>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81-Transactional%E6%B3%A8%E8%A7%A3%E4%B8%8E%E4%BA%8B%E5%8A%A1%E4%BC%A0%E6%92%AD%E8%A1%8C%E4%B8%BA"><span class="toc-text">一、@Transactional注解与事务传播行为</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%AE%9E%E9%99%85%E5%9C%BA%E6%99%AF%EF%BC%9A%E7%88%B6%E4%BA%8B%E5%8A%A1%E4%B8%AD%E6%8E%92%E9%99%A4%E9%83%A8%E5%88%86%E5%AD%90%E4%BA%8B%E5%8A%A1"><span class="toc-text">二、实际场景：父事务中排除部分子事务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A%E4%BD%BF%E7%94%A8-Transactional-propagation-Propagation-NOT-SUPPORTED"><span class="toc-text">三、解决方案：使用@Transactional(propagation &#x3D; Propagation.NOT_SUPPORTED)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%88%B6Service"><span class="toc-text">1. 父Service</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%AD%90Service"><span class="toc-text">2. 子Service</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E6%B5%8B%E8%AF%95%E4%B8%8E%E7%BB%93%E6%9E%9C"><span class="toc-text">3. 测试与结果</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E6%80%BB%E7%BB%93"><span class="toc-text">四、总结</span></a></li></ol>
  </aside>

          
        </div>
      </div>
    
  </div>
</main>
  

<footer class="footer">
  <div class="footer-social"><a 
        href="tencent://message/?Menu=yes&uin=283371828 "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#12B7F5'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconQQ "></i>
      </a><a 
        href="javascript:; "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#09BB07'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconwechat-fill "></i>
      </a><a 
        href="https://github.com/scwang90 "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#9f7be1'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  icongithub-fill "></i>
      </a><a 
        href="mailto:scwang90@hotmail.com "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color=#FF3B00" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconmail"></i>
      </a></div>
  
    <div class="footer-copyright"><p>Powered by <a target="_blank" href="https://github.com/scwang90">scwang90</a>  |  Theme - <a target="_blank" href="https://github.com/izhaoo/hexo-theme-zhaoo">zhaoo</a></p> <p><a target="_blank" href="http://beian.miit.gov.cn/"><span>黔ICP备2021004317号-1</span></a><span>  |  </span><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img class="lazyload" data-original="/images/icons/icon-filing.png" src="https://www.beian.gov.cn/img/new/gongan.png"/> <span> 贵公网安备 52010202002539号</span></a></p></div>
  
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
  
  
    
<script src="/js/color-mode.js"></script>

  
  
</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>





  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




  
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>






  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/script.js"></script>







  <script>
    (function () {
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      } else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>













</html>